﻿using System.Drawing;
using System.Linq;
using RRRSRoguelike.Entities;
using RRRSRoguelike.Entities.Props;
using RRRSRoguelike.Enums;
using RRRSRoguelike.Managers;

namespace RRRSRoguelike.Helpers.Actions
{
	public static class ActionHelper
	{
		public static void Get(Point position)
		{

			if (DungeonManager.Instance.PropManager.Props.OfType<Weapon>().Any(p => p.Position == position))
			{

				if (DungeonManager.Instance.CreatureManager.Player.WeaponSlot != null)
					Put(position);

				var weapon = DungeonManager.Instance.PropManager.GetBestWeapon(position);
				DungeonManager.Instance.CreatureManager.Player.AddWeapon(weapon);
				DungeonManager.Instance.PropManager.Remove(weapon);
			}
		}

		public static void Put(Point point)
		{
			if (DungeonManager.Instance.CreatureManager.Player.WeaponSlot != null)
			{
				var weapon = DungeonManager.Instance.CreatureManager.Player.WeaponSlot;
				weapon.Position = DungeonManager.Instance.CreatureManager.Player.Position;
				DungeonManager.Instance.CreatureManager.Player.RemoveWeapon();
				DungeonManager.Instance.PropManager.Add(weapon);
			}
		}

		public static bool Remove()
		{
			//shortens calls.
			var ActionManager = DungeonManager.Instance.ActionManager;
			var player = DungeonManager.Instance.CreatureManager.Player;

			var built = false;
			var build = true;

			while (build)
			{
				var direction = DungeonManager.Instance.DungeonScreen.GetDirectionFromUser();

				var newPosition = DungeonLevel.GetNewPosition(player.Position, direction, 1);

				var tile = (Tile)DungeonManager.Instance.DungeonLevel.GetTile(newPosition, Layers.Prop);

				if (tile != null)
				{
					DungeonManager.Instance.PropManager.Remove(tile);
					built = true;
					build = false;
					DungeonManager.Instance.ForceRedraw();
				}

				else if (ActionManager.IsActionPushed(Enums.Actions.Quit) ||
						 ActionManager.IsActionPushed(Enums.Actions.Build))
				{
					build = false;
				}
			}
			return built;
		}

		public static bool Build(Tile tile)
		{
			//shortens calls.
			var ActionManager = DungeonManager.Instance.ActionManager;
			var player = DungeonManager.Instance.CreatureManager.Player;

			var built = false;
			var build = true;

			while (build)
			{
				var direction = DungeonManager.Instance.DungeonScreen.GetDirectionFromUser();

				var newPosition = DungeonLevel.GetNewPosition(player.Position, direction, 1);

				if (DungeonManager.Instance.DungeonLevel.IsOKToPlaceMonster(newPosition))
				{
					tile.Position = newPosition;
					DungeonManager.Instance.PropManager.Add(tile);
					DungeonManager.Instance.ForceRedraw();
					built = true;
					build = false;

				}
				else if (ActionManager.IsActionPushed(Enums.Actions.Quit) ||
					 ActionManager.IsActionPushed(Enums.Actions.Build))
				{
					build = false;
				}
			}
			return built;
		}
				
		//this is an action and doesn't belong here!!!
		public static void ShootFireball(Point position,Direction direction)
		{
			DungeonManager.Instance.PropManager.Fireball = new Fireball(position, direction);
			DungeonManager.Instance.PropManager.Fireball.Reset();
			// should be elsewhere --> this.Flags["hasFired"] = true;
		}
	}
}
